package middleware

import (
	"go-papers/handler"
	jwt "go-papers/pkg/jwtauth"
	"go-papers/tools/config"
	"time"
)

func AuthInit() (*jwt.GinJWTMiddleware, error) {
	return jwt.New(&jwt.GinJWTMiddleware{
		CookieName:      "admin_jwt",
		Realm:           "admin_papers",
		Key:             []byte(config.GConfig.JwtConfig.Secret),
		Timeout:         24 * time.Hour,
		MaxRefresh:      24 * time.Hour,
		PayloadFunc:     handler.PayloadFunc,
		IdentityHandler: handler.IdentityHandler,
		Authenticator:   handler.Authenticator,
		Authorizator:    handler.Authorizator,
		Unauthorized:    handler.Unauthorized,
		TokenLookup:     "header: Authorization, query: token, cookie: jwt,param: token",
		TokenHeadName:   "Bearer",
		TimeFunc:        time.Now,
	})

}

func AuthGatewayInit() (*jwt.GinJWTMiddleware, error) {
	return jwt.New(&jwt.GinJWTMiddleware{
		CookieName:      "gateway_jwt",
		Realm:           "gateway_papers",
		Key:             []byte(config.GConfig.JwtConfig.Secret),
		Timeout:         240 * time.Hour,
		MaxRefresh:      240 * time.Hour,
		PayloadFunc:     handler.GatewayPayloadFunc,
		IdentityHandler: handler.GatewayIdentityHandler,
		Authenticator:   handler.GatewayAuthenticator,
		Authorizator:    handler.GatewayAuthorizator,
		Unauthorized:    handler.Unauthorized,
		TokenLookup:     "header: Authorization, query: token, cookie: jwt,param: token",
		TokenHeadName:   "Bearer",
		TimeFunc:        time.Now,
	})

}
